package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsda.util.Util;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class DeleteProgram
 */
@WebServlet("/DeleteProgram")
public class DeleteProgram extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeleteProgram() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String prgName = request.getParameter("PrgName");
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		Connection conn = null;
		try {
			conn = Util.getConnection();
			Util.deleteProgram(conn, prgName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e.getMessage());
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (Exception ignore) {
			}
		}
		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
//		Enumeration<String> es = request.getParameterNames();
//
//		while (es.hasMoreElements()) {
//			String name = es.nextElement();
//			System.out.println("deletejob " + name + "="
//					+ request.getParameter(name));
//		}
	}

}
